import subprocess
from pathlib import Path
from core.backup import BackupManager
from plugins.template.adapter import PluginBase

class ArchPlugin(PluginBase):
    def __init__(self):
        self.mirrorlist_path = Path("/etc/pacman.d/mirrorlist")
        self.backup = BackupManager("arch")

    @classmethod
    def is_compatible(cls) -> bool:
        return Path("/etc/arch-release").exists()

    def apply_mirror(self, mirror_url: str, top_n: int = 5):
        """使用 reflector 生成最优镜像列表"""
        self.backup.create(self.mirrorlist_path)
        
        # 调用 reflector 测速并生成新配置
        subprocess.run([
            "reflector",
            "--protocol", "https",
            "--country", "China",
            "--latest", str(top_n),
            "--sort", "rate",
            "--save", str(self.mirrorlist_path)
        ], check=True)